package com.fenghuajueli.libbasecoreui;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.view.Gravity;

import androidx.appcompat.app.AppCompatDelegate;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.MetaDataUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.lib_net.common.RetrofitUtils;
import com.fenghuajueli.lib_statistics.UmEventUtils;
import com.fenghuajueli.libbasecoreui.constants.ConfigConstants;
import com.fenghuajueli.libbasecoreui.constants.SwitchConfig;
import com.fenghuajueli.libbasecoreui.upload.OnActionChangeListener;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;
import com.ijj.tookit.file.IFileUtils;
import com.tencent.mmkv.MMKV;
import com.umeng.commonsdk.UMConfigure;
import com.uyumao.nns.zmd.ZmdManager;

import java.util.HashMap;

public abstract class BaseApplication extends Application implements OnActionChangeListener {

    private static BaseApplication application;

    public static BaseApplication getApplication() {
        return application;
    }

    /**
     * 是否是debug模式
     *
     * @return
     */
    public abstract boolean appIsDebug();

    @AppCompatDelegate.NightMode
    public abstract int getDayNightMode();

    /**
     * 获取渠道名称
     *
     * @return
     */
    public abstract String getChannel();

    public abstract String getHttpHost();

    /**
     * 返回友盟的key，隐私合规整改后用于预先初始化
     *
     * @return
     */
    public String getUmAppKey() {
        return MetaDataUtils.getMetaDataInApp("UMENG_APPKEY");
    }


    /**
     * 初始化第三方的sdk,目前模板中只有友盟会调用初始化
     */
    public void initThirdSdk() {
        initUm();
        RetrofitUtils.init(this, getHttpHost(), () -> {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("User-Agent", getPackageName() + "/" + getVersionName());
            hashMap.put("AppVersionCode", AppUtils.getAppVersionCode() + "");
            hashMap.put("AppVersionName", AppUtils.getAppVersionName());
            return hashMap;
        });
    }

    /**
     * 初始化正常不会调用隐私相关的sdk
     */
    protected void initNormalSdk() {
        if (appIsDebug()) {           // 这两行必须写在init之前，否则这些配置在init过程中将无效
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ToastUtils.getDefaultMaker().setGravity(Gravity.CENTER, 0, 0);
        ToastUtils.getDefaultMaker().setBgColor(Color.BLACK);
        ToastUtils.getDefaultMaker().setTextColor(Color.WHITE);
        LogUtils.getConfig().setLogSwitch(appIsDebug());
        SwitchConfig.currentChannel = getChannel();
        MMKV.initialize(this);
        ARouter.init(this);
        UmEventUtils.init(this);
        // 文件保存工具
        IFileUtils.Companion.getInstance().init(this);
    }


    private void initUm() {
        //友盟预初始化 根据官方文档所写 不会调用任何隐私api 也不会上报
        UMConfigure.preInit(application, getUmAppKey(), getChannel());
        UMConfigure.setLogEnabled(appIsDebug());
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "");
        //友盟合规助手
        ZmdManager.init(this, getUmAppKey(), () -> {
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        AppCompatDelegate.setDefaultNightMode(getDayNightMode());

        initNormalSdk();
        if (MmkvUtils.get(ConfigConstants.MMKVKEY.privacyDialogIsShow.name(), false)) {
            initThirdSdk();
        }
    }


    private String getVersionName() {
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            return packageInfo.versionName;
        } catch (Exception e) {
            return "unKnow Version";
        }
    }
}
